home *** CD-ROM | disk | FTP | other *** search
/ CD Actual 3 / CD ACTUAL 3.iso / linux / sonido / sfxserve.000 / sfxserve / sfxserver-0.02 / sample.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-01-10  |  3.9 KB  |  150 lines

  1. /*
  2.  * ---------------------------------------------------------------------------
  3.  * sfxserver/sample.c
  4.  *
  5.  * Copyright by Terry Evans 1994
  6.  * tevans@cs.utah.edu, tevans@slc.unisys.com
  7.  * ---------------------------------------------------------------------------
  8.  *
  9.  * Redistribution and use in source and binary forms, with or without
  10.  * modification, are permitted provided that the following conditions are
  11.  * met: 1. Redistributions of source code must retain the above copyright
  12.  * notice, this list of conditions and the following disclaimer. 2.
  13.  * Redistributions in binary form must reproduce the above copyright notice,
  14.  * this list of conditions and the following disclaimer in the documentation
  15.  * and/or other materials provided with the distribution.
  16.  *
  17.  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND ANY
  18.  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  19.  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  20.  * DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
  21.  * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  22.  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  23.  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  24.  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  25.  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  26.  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  27.  * SUCH DAMAGE.
  28.  * ---------------------------------------------------------------------------
  29.  */
  30.  
  31. #include <sys/types.h>
  32. #include <sys/stat.h>
  33. #include <stdio.h>
  34. #include <stdlib.h>
  35. #include <unistd.h>
  36.  
  37.  
  38. #include "global.h"
  39. #include "types.h"
  40. #include "error.h"
  41. #include "io.h"
  42. #include "sample.h"
  43.  
  44.  
  45. /* Current sample number */
  46. static unsigned int sample_number;
  47.  
  48.  
  49. void S_Init(sample_t **sample)
  50. {
  51.   int loop;
  52.  
  53.   IO_WriteStdout("        Setting up sample data structures\n");
  54.  
  55.   for(loop = 0; loop < MAX_SAMPLES; loop++)
  56.     sample[loop] = NULL;
  57.  
  58.   /* Initialize the sample number */
  59.   sample_number = 0;
  60. }
  61.  
  62.  
  63. void S_DeInit(sample_t **sample)
  64. {
  65.   int loop;
  66.  
  67.   for(loop = 0; loop < sample_number; loop++)
  68.   {
  69.     if(sample[loop] != NULL)
  70.     {
  71.       /* Free that data for the sound */
  72.       if(sample[loop]->data != NULL)
  73.     free(sample[loop]->data);
  74.  
  75.       /* Free the sound structure */
  76.       free(sample[loop]);
  77.       sample[loop] = NULL;
  78.     }
  79.   }
  80.  
  81.   sample_number = 0;
  82. }
  83.  
  84.  
  85. int S_LoadRawSample(const char *file_name, sample_t **sample)
  86. {
  87.   FILE *fp;
  88.  
  89.   /* Open the file for the sound */
  90.   fp = fopen(file_name, "r");
  91.    
  92.   if(fp == NULL)
  93.   {
  94.     E_ErrnoNonFatalError("S_LoadRawSample", "Could not load sound file %s", file_name);
  95.     return(-1);
  96.   }
  97.    
  98.   /* See if we need to allocate for the sample number */
  99.   if(sample[sample_number] == NULL)
  100.   {
  101.     if((sample[sample_number] = (sample_t *)malloc(sizeof(sample_t))) == NULL)
  102.     {
  103.       E_NonFatalError("S_LoadRawSample", "Could not malloc %ul bytes", sizeof(sample_t));
  104.       return(-1);
  105.     }
  106.   }
  107.  
  108.   else
  109.   {
  110.     E_NonFatalError("S_LoadRawSample", "Sample slot %s already in use", sample_number);
  111.     return(-1);
  112.   }
  113.  
  114.   sample[sample_number]->data = NULL;
  115.   sample[sample_number]->length  = 0;
  116.    
  117.   /* Get the length of the file */
  118.   sample[sample_number]->length = lseek(fileno(fp), 0, SEEK_END);
  119.  
  120.   /* go back to beginning of file */
  121.   lseek(fileno(fp), 0, SEEK_SET);
  122.  
  123.   /* alloc memory for sample */
  124.   sample[sample_number]->data = 
  125.     (unsigned char *)malloc(sample[sample_number]->length);
  126.    
  127.   if(sample[sample_number]->data == NULL)
  128.   {
  129.     fclose(fp);
  130.     free(sample[sample_number]);
  131.     E_NonFatalError("S_LoadRawSample", "No sound data found in %s", file_name);
  132.     return(-1);
  133.   }
  134.    
  135.   fread(sample[sample_number]->data, 1, sample[sample_number]->length, fp);
  136.   fclose(fp);
  137.  
  138.   return(sample_number++);
  139. }
  140.  
  141.  
  142. int S_VerifySample(int number)
  143. {
  144.   if(number < sample_number)
  145.     return(TRUE);
  146.   else
  147.     return(FALSE);
  148. }
  149.       
  150.